package edu.sctu.zhu.controller;


import com.google.gson.Gson;
import edu.sctu.zhu.bean.Fans;
import edu.sctu.zhu.dao.FansDao;
import edu.sctu.zhu.util.HttpClientUtil;
import edu.sctu.zhu.weixing.AccessTokenUtil;
import edu.sctu.zhu.weixing.bean.Openid;
import edu.sctu.zhu.weixing.bean.WeixingUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by zhu on 2015/10/31.
 */
@Controller
public class ZhuIndexController {

    @Autowired
    private FansDao fansDao;

    @RequestMapping(value = "/zhu/upload",method = RequestMethod.GET)
    public String UploadRturb(){

        return "/zhu/register";
    }

    @RequestMapping(value = "/zhu/shelves",method = RequestMethod.GET)
    public String shelvesRturb(){

        return "/zhu/shelves";
    }

    @RequestMapping(value = "/zhu/shelves_Upload",method = RequestMethod.GET)
    public String shelvesb(){

        return "/back_stage_anagement/upload";
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){

        return "/register";
    }

    @RequestMapping(value = "/showGoods",method = RequestMethod.GET)
    public String showGoods(){

        return "/zhu/showGoods";
    }

//    @RequestMapping(value = "/weixingFans",method = RequestMethod.GET)
//    public String weixingFans(){
//
//        return "/weixingFans";
//    }

 @RequestMapping(value = "/zhu/pushCarts",method = RequestMethod.GET)
    public String pushCarts(){

        return "/zhu/pushCarts";
    }

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String getTest(){

        return "/test";
    }

 @RequestMapping(value = "/zhu/pushorder",method = RequestMethod.GET)
    public String pushorder(){

        return "/zhu/pushorder";
    }

    @RequestMapping(value = "/myTest",method = RequestMethod.GET)
    public String myTest(){

        return "myTest";
    }

    @RequestMapping(value = "/fans/addAddress",method = RequestMethod.GET)
    public String address(){

        return "/fans/add_address";
    }
//
//    @RequestMapping(value = "/loginWeixin", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
//    public String loginWeixin(ServletRequest request, HttpSession session) {
//
//        String weixin = request.getParameter("weixin");
//        String name = request.getParameter("name");
//
//
//        try {
//            Fans fans;
//            fans = fansDao.findByWeixin(weixin);
//            if (fans == null) {
//                fans = new Fans();
//                fans.setWeixing(weixin);
//                fans.setName(name);
//                fansDao.save(fans);
//                fans = fansDao.findByWeixin(weixin);
//            }
//            session.setAttribute("weixin", fans);
//        } catch (Exception e) {
//
//            return "myTest";
//        }
//
//        return "/goods/index";
//    }


    @RequestMapping(value = "/authorization", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String authorization ( @RequestParam(required = false) String code, @RequestParam(required = false) String state, HttpSession session) {
        Gson gson = new Gson();
        String result = null;
        try {
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx6fd8fa52f64296ef&secret=9b36650f00088a17b7baa8b722e12a72&code="+code+"&grant_type=authorization_code";
            HttpClientUtil httpClientUtil = new HttpClientUtil();
            result = httpClientUtil.doGetSSL(url);
            Openid openid = gson.fromJson(result, Openid.class);
            String url2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ AccessTokenUtil.access_token+"&openid="+openid.getOpenid()+"&lang=zh_CN";
            result = httpClientUtil.doGetSSL(url2);
            WeixingUser weixingUser = gson.fromJson(result, WeixingUser.class);

            Fans fans;
            fans = fansDao.findByWeixin(weixingUser.getOpenid());
            if (fans == null&&weixingUser.getOpenid()!=null) {
                fans = new Fans();
                fans.setWeixing(weixingUser.getOpenid());
                fans.setName(weixingUser.getNickname());
                fansDao.save(fans);
                fans = fansDao.findByWeixin(weixingUser.getOpenid());
            }
            session.setAttribute("weixin", fans);

            return "/goods/index";
        } catch (Exception e) {
            return "/goods/index";
        }
    }

}
